#include "locus_frequency.h"

LocusFrequency::LocusFrequency() {
    _n = 0;
    _nHo = 0;
    _Ho = 0;
}

void LocusFrequency::addLocus( Locus *locus ) {
    for(int i=0;i<locus->ploidy();i++){
        QString allele = locus->getAllele(i);
        if( !_alleles.keys().contains( allele ))
            _alleles[allele] = 0;
        _alleles[allele] += 1.0;
    }
    _n += 1;
    if( locus->isHeterozygote()) _Ho++;
    if( locus->ploidy() > 2 ) _nHo++;
}

qreal LocusFrequency::getFrequency(QString allele){
    if( !_alleles.keys().contains(allele) || _n==0)
        return 0.0;
    else
        return _alleles[allele] / _n;
}

qreal LocusFrequency::getHe(){
    if( _nHo )
        return _Ho / _nHo;
    else
        return 0.0;
}

qreal LocusFrequency::getHo(){
    qreal ret = 0.0;
    foreach(QString allele, _alleles.keys()){
        qreal f = getFrequency( allele );
        ret += f*f;
    }
    return 1.0 - ret;
}
